module testgame(

		sw, key, rst, clock50, CLOCK_27, TD_RESET,
		ledr, ledg,
		hex0, hex1, hex2, hex3, hex4, hex5, hex6, hex7,
		keyboard_clk, keyboard_data,
		LCD_DATA, LCD_ON, LCD_BLON, LCD_RW, LCD_EN, LCD_RS,
		VGA_CLK,
				VGA_HS,
				VGA_VS,
				VGA_BLANK,
				VGA_SYNC,
				VGA_R,
				VGA_G,
				VGA_B
);

input CLOCK_27;     //      27 MHz
 output TD_RESET;	//	TV Decoder Reset
 output VGA_CLK;   						//	VGA Clock
  output VGA_HS;							//	VGA H_SYNC
  output VGA_VS;							//	VGA V_SYNC
  output VGA_BLANK;						//	VGA BLANK
  output VGA_SYNC;						//	VGA SYNC
  output [9:0] VGA_R;   						//	VGA Red[9:0]
  output [9:0] VGA_G;	 						//	VGA Green[9:0]
  output [9:0] VGA_B;  	
 
	// Inputs/outputs
			// System inputs
			input[17:0] sw;
			input[3:1] key; // UNUSED
			input clock50;
			input rst;

			// System outputs
			output[17:0] ledr;
			output[8:0] ledg;

			// Hex display outputs
			output[6:0] hex0;
			output[6:0] hex1;
			output[6:0] hex2;
			output[6:0] hex3;
			output[6:0] hex4;
			output[6:0] hex5;
			output[6:0] hex6;
			output[6:0] hex7;

			// Keyboard inputs
			input keyboard_clk;
			input keyboard_data;

			// 16x2 LCD display outputs
			output[7:0] LCD_DATA;
			output LCD_ON; // LCD Power ON/OFF
			output LCD_BLON;  // LCD Backlight ON/OFF
			output LCD_RW; // LCD Read/Write Select, 0 = Write, 1 = Read
			output LCD_EN; // LCD Enable
			output LCD_RS; // LCD Command/Data Select, 0 = Command, 1 = Data
			
			wire[16:0] keydata;
			reg[15:0] entry;
			wire[127:0] phrase0; // line 0 of the phrase
			wire[127:0] phrase1; // line 1 of the phrase
<<<<<<< .mine
			wire[127:0] disp0; // for use in guessing stage
			wire[127:0] disp1; // for use in guessing stage
=======
			wire[9:0] guess;
>>>>>>> .r129
			parameter ENTER = 16'hF05A;
			parameter pressedN = 16'hF031;
			parameter pressedY = 16'hF035;
			parameter pressedBACK = 16'hF066;
			
			always@(*)
			begin
				if(keydata[16] == 1'b1)
					entry = keydata;
				else entry = 16'h0000;
			end
			
			parameter	resetstate	= 4'd0;
			parameter	initstate	= 4'd1;
			parameter	waittostart	= 4'd2;
			parameter	phraseentry	= 4'd3;
			parameter	guessing		= 4'd4;
			parameter	p2lost		= 4'd5;
			parameter	p2won			= 4'd6;
			parameter	gameover		= 4'd7;
			parameter	thanks		= 4'd8;
			
			reg[3:0] s;
			reg[3:0] ns;
			reg[29:0] count;
			reg[1:0] editphrase;
			reg[1:0] acceptguesses;
			wire phrasestarted;
			wire [1:0] gameoutcome;
			
			wire diekey;
			wire rst2;
			
always@(*)
begin
	case(s)
		
		resetstate:
		begin
			if(count == 1'b0)
				ns = initstate;
			else ns = resetstate;
		end
		
		initstate:
		begin
			if(entry == ENTER)
			begin
				ns = waittostart;
			end
			else
			begin
				ns = initstate;
			end
		end
		
		waittostart:
		begin
			if(entry == ENTER)
			begin
				ns = phraseentry;
				//rst2 = 1'b0;
			end
			else
			begin
				ns = waittostart;
			end
		end
		
		phraseentry:
		begin
//			if(entry == ENTER)
//				if(phrasestarted == 1'b1)
//					if(phrase0 != 0 && phrase1 != 0)
//						ns = guessing;
			if(entry == ENTER && phrasestarted == 1'b1 && phrase0 != 0 && phrase1 != 0)
			begin
				//if (phrase0 != 127'h20202020_20202020_20202020_20202020 && phrase1 != 127'h20202020_20202020_20202020_20202020)
				begin
					ns = guessing;
				end
			end
			else ns = phraseentry;
		end
		
		guessing:
		begin
			if(gameoutcome == 2'b01)
				ns = p2lost;
			else if (gameoutcome == 2'b10)
				ns = p2won;
			else ns = guessing;
		end
		
		p2lost:
		begin
			if(entry == ENTER)
				ns = gameover;
			else ns = p2lost;
		end
		
		p2won:
		begin
			if(entry == ENTER)
				ns = gameover;
			else ns = p2won;
		end
		
		gameover:
		begin
			if(entry == pressedN)
				ns = thanks;
			else if (entry == pressedY)
			begin
<<<<<<< .mine
				ns = waittostart;
				//rst2 = 1'b1;
=======
				case(s)
					
					resetstate:
					begin
						count = 1'b0;
						displaymode = 4'h0;
						editphrase = 2'b00;
					end
					
					initstate:
					begin
						count = 1'b1;
						displaymode = 4'h1;
						editphrase = 2'b00;
					end
					
					waittostart:
					begin
						count = 1'b0;
						displaymode = 4'h2;
						editphrase = 2'b00;
					end
					
					instr:
					begin
						if(sw[17] == 1'b0)
							displaymode = 4'h3;
						else displaymode = 4'h4;
						editphrase = 2'b01;
					end
					
					phraseentry:
					begin
						editphrase = 2'b01;
						displaymode = 4'h4;
					end
					
					guessing:
					begin
						editphrase = 2'b10;
						if(sw[17] == 1'b0)
							displaymode = 4'h5;
						else displaymode = 4'h4;
					end
					
					p2lost:
					begin
						editphrase = 2'b10;
						displaymode = 4'h6;
					end
					
					p2won:
					begin
						editphrase = 2'b10;
						displaymode = 4'h7;
					end
					
					gameover:
					begin
						editphrase = 2'b10;
						displaymode = 4'h8;
					end
					
					thanks:
					begin
						editphrase = 2'b10;
						displaymode = 4'h9;
					end
					
				endcase
>>>>>>> .r129
			end
			else ns = gameover;
		end
		
		thanks:
		begin
			ns = thanks;
		end
	endcase
end

always@(posedge clock50 or negedge rst)
begin
	if (rst == 1'b0)
	begin
		s <= resetstate;
	end
	else
	begin
		s <= ns;
	end
end

always@(*)
begin
	case(s)
		
		resetstate:
		begin
			count = 1'b0;
			displaymode = 4'h0;
			editphrase = 2'b00;
			acceptguesses = 2'b00;
		end
		
		initstate:
		begin
			count = 1'b1;
			displaymode = 4'h1;
			editphrase = 2'b00;
			acceptguesses = 2'b00;
		end
		
		waittostart:
		begin
			count = 1'b0;
			displaymode = 4'h2;
			editphrase = 2'b01;
			acceptguesses = 2'b01;
		end
		
		phraseentry:
		begin
			displaymode = 4'h3;
			editphrase = 2'b10;
			acceptguesses = 2'b00;
		end
		
		guessing:
		begin
			if(sw[17] == 1'b1 && sw[16] == 1'b0)
				displaymode = 4'h3;
			else if(sw[17] == 1'b0 && sw[16] == 1'b1)
				displaymode = 4'h4;
			else displaymode = 4'h9;
			editphrase = 2'b11;
			acceptguesses = 2'b10;
		end
		
		p2lost:
		begin
			editphrase = 2'b11;
			displaymode = 4'h5;
			acceptguesses = 2'b11;
		end
		
		p2won:
		begin
			editphrase = 2'b11;
			displaymode = 4'h6;
			acceptguesses = 2'b11;
		end
		
		gameover:
		begin
			editphrase = 2'b11;
			displaymode = 4'h7;
			acceptguesses = 2'b11;
		end
		
		thanks:
		begin
			editphrase = 2'b11;
			displaymode = 4'h8;
			acceptguesses = 2'b11;
		end
		
	endcase
end
			
			reg[3:0] displaymode;
			lcdtop lt(
					displaymode, keydata, editphrase, entry, acceptguesses, phrasestarted, gameoutcome,
					rst, clock50,
					keyboard_clk, keyboard_data,
					LCD_DATA, LCD_ON, LCD_BLON, LCD_RW, LCD_EN, LCD_RS,
					phrase0, phrase1, hex2, hex3, hex4, hex5, hex6, hex7, diekey
			);
			
			vgalab2(
				clock50,
				CLOCK_27,
				key,
				sw,
				ledg,
				ledr,
				TD_RESET,
				VGA_CLK,
				VGA_HS,
				VGA_VS,
				VGA_BLANK,
				VGA_SYNC,
				VGA_R,
				VGA_G,
				VGA_B,
				diekey,
				rst,
				rst2
			);
			
			hexDisplay disp00(s, hex0);
			hexDisplay disp01({3'b000, phrasestarted}, hex1);

endmodule